//package com.example.demo.redis;
//
//import com.fasterxml.jackson.annotation.JsonAutoDetect;
//import com.fasterxml.jackson.annotation.PropertyAccessor;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
//import redis.clients.jedis.JedisPoolConfig;
//
///**
// * Created by wuchang on 2019/2/12.
// */
//@Configuration
//public class RedisBaseConfig {
//    private int redisPoolMaxActive = 1000;
//    private int redisPoolMaxIdle = 50;
//    private int redisPoolMinIdle = 50;
//    private int redisPoolMaxWait = 2000;
//
//    /**
//     * 创建连接
//     * @param host
//     * @param port
//     * @param timeout
//     * @return
//     */
//    public JedisConnectionFactory newJedisConnectionFactory(int index,String host,int port, String password,int timeout){
//        JedisConnectionFactory factory = new JedisConnectionFactory();
//        factory.setDatabase(index);
//        factory.setHostName(host);
//        factory.setPort(port);
//        factory.setPassword(password);
//        factory.setTimeout(timeout); //设置连接超时时间
//
//        //testOnBorrow为true时，返回的连接是经过测试可用的
//        factory.setPoolConfig(poolConfig(redisPoolMaxIdle,redisPoolMinIdle,redisPoolMaxActive,redisPoolMaxWait,true));
//        return factory;
//    }
//
//    public JedisPoolConfig poolConfig(int maxIdle, int minIdle,int maxTotal,long maxWaitMillis,boolean testOnBorrow) {
//        JedisPoolConfig poolCofig = new JedisPoolConfig();
//        poolCofig.setMaxIdle(maxIdle);
//        poolCofig.setMinIdle(minIdle);
//        poolCofig.setMaxTotal(maxTotal);
//        poolCofig.setMaxWaitMillis(maxWaitMillis);
//        poolCofig.setTestOnBorrow(testOnBorrow);
//        return poolCofig;
//    }
//
//    public void setSerializer(RedisTemplate template) {
//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//        ObjectMapper om = new ObjectMapper();
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        jackson2JsonRedisSerializer.setObjectMapper(om);
//        template.setValueSerializer(jackson2JsonRedisSerializer);
//        template.setKeySerializer(new StringRedisSerializer());
//    }
//}
